import { _decorator, Component, Label, Button, ProgressBar, Node, director, tween, log, Material, Sprite, InstanceMaterialType, builtinResMgr, UIRenderer, sys } from 'cc';
const { ccclass, property, executionOrder } = _decorator;

import CardFunctions from "../Common/CardFunctions";
import { Lockable } from "../Common/Lockable";
import { Consts } from "../Data/Consts";
import { GameData } from "../Data/GameData";
import { MainState } from "../Data/GameEnum";
import GameStorage from "../Data/GameStorage";
import SS_UIManager from "../Manager/SS_UIManager";
import SS_NodeManager from "../Manager/SS_NodeManager";
import CommonFunction from "../utils/CommonFunction";
import MainPanel from "./MainPanel";
import SettingPanel from "./SettingPanel";
import { SS_AudioManager } from '../Manager/SS_AudioManager';

@ccclass('GamePanel')
@executionOrder(103)
export default class GamePanel extends Component {
    static Instance: GamePanel;
    @property(Label)
    Label_level: Label | null = null;
    @property(Button)
    Button_replay: Button | null = null;
    @property(Button)
    Button_setting: Button | null = null;
    @property(Button)
    Button_随机方块: Button | null = null;
    @property(Button)
    Button_摧毁障碍: Button | null = null;
    @property(ProgressBar)
    ProgressBar: ProgressBar | null = null;
    @property(Node)
    emoji: Node | null = null;

    @property(Node)
    连击: Node = null;
    @property(Label)
    Label_连击: Label | null = null;
    @property([Node])
    解锁格子: Node[] = [];

    @property(Node)
    下一个俄罗斯方块: Node = null;

    @property(Node)
    引导手_拖拽: Node = null;
    @property(Node)
    引导手_点击旋转: Node = null;

    @property(Node)
    无尽模式开场动画: Node = null;

    @property(Button)
    Button_选关: Button | null = null;
    GeneratePoint_blook_list = [];
    GeneratePoint_blook_list_temp = [];

    tween_引导手
    onLoad() {
        GamePanel.Instance = this;

        this.GeneratePoint_blook_list = SS_NodeManager.Instance.GeneratePoint_blook.children;
        this.GeneratePoint_blook_list_temp = SS_NodeManager.Instance.GeneratePoint_blook_temp.children;
    }
    ShowOn() {
        this.node.active = true;

        // CardFunctions.禁止操作(true);

        this.AddListener();
        this.Refresh();

        this.Button_选关.node.active = sys.isBrowser;
    }
    ShowOff() {
        this.node.active = false;
        this.RemoveListener();
    }
    Refresh() {
        this.Refresh_level();
        // this.Refresh_progress(0);
        this.Refresh_解锁格子();
        this.Refresh_Button_摧毁障碍();
    }
    Refresh_level() {
        let iswujin = CardFunctions.是否是无尽模式(GameData.Get_CurrentLevel())
        if (iswujin) {
            let num = CardFunctions.获取当前关卡是无尽第几关(GameData.Get_CurrentLevel())
            console.log("GameData.Get_CurrentLevel()", GameData.Get_CurrentLevel());
            console.log("num", num);

            this.Label_level.string = `无尽模式第${num + 1}关`;
        }
        else {
            this.Label_level.string = `第${GameData.Get_CurrentLevel() + 1}关`;
        }
    }

    Refresh_解锁格子() {
        let item = CardFunctions.获取解锁格子记录();

        for (let index = 0; index < item.unlock.length; index++) {
            const isunlock = item.unlock[index];
            let 解锁格子 = this.解锁格子[index];
            解锁格子.active = !isunlock;
        }
    }

    Refresh_Button_摧毁障碍() {
        let array = GameData.arrStopBlock.filter(x => x.type === 1)
        if (array && array.length > 0) {
            this.Button_摧毁障碍.interactable = true;
        }
        else {
            this.Button_摧毁障碍.interactable = false;
        }

        CommonFunction.SetGray(this.Button_摧毁障碍.node, !this.Button_摧毁障碍.interactable);
    }

    Refresh_progress(_duration) {
        let array = [0, 0.33, 0.66, 1, 100];
        let progress = GameData.destroy_card_count / GameData.total_card_count;

        tween(this.ProgressBar)
            .to(_duration, { progress: progress })
            .call(() => {
                this.ProgressBar.progress = progress;
            })
            .start();

        CommonFunction.SetAllChildrenAtive(this.emoji, false);
        for (let index = 0; index < array.length; index++) {
            const min = array[index];
            const max = array[index + 1];
            if (progress >= min && progress < max) {
                this.emoji.children[index].active = true;
                return;
            }
        }
    }

    AddListener() {
        this.Button_replay.node.on("click", this.On_Button_replay, this);
        this.Button_setting.node.on("click", this.On_Button_setting, this);
        this.Button_随机方块.node.on("click", this.On_Button_随机方块, this);
        this.Button_摧毁障碍.node.on("click", this.On_Button_摧毁障碍, this);

        this.解锁格子[0].on("click", this.On_解锁格子1, this);
        this.解锁格子[1].on("click", this.On_解锁格子2, this);

        this.Button_选关.node.on("click", this.On_Button_选关, this);
    }
    RemoveListener() {
        this.Button_replay.node.off("click", this.On_Button_replay, this);
        this.Button_setting.node.off("click", this.On_Button_setting, this);
        this.Button_随机方块.node.off("click", this.On_Button_随机方块, this);
        this.Button_摧毁障碍.node.off("click", this.On_Button_摧毁障碍, this);

        this.解锁格子[0].off("click", this.On_解锁格子1, this);
        this.解锁格子[1].off("click", this.On_解锁格子2, this);

        this.Button_选关.node.off("click", this.On_Button_选关, this);
    }

    看广告解锁格子(_index) {
        let item = CardFunctions.获取解锁格子记录();

        item.unlock[_index] = true;
        GameStorage.saveData();

        this.Refresh_解锁格子();

        let map_data = GameData.UnlockData[_index];
        for (let index = 0; index < map_data.length; index++) {
            let item_map_data = map_data[index];
            let block = CardFunctions.GetStopBlock(item_map_data.x, item_map_data.y);
            block.Destroy();
        }
    }

    async On_解锁格子1() {
        if (GameData.mainState !== MainState.空闲) {
            return;
        }

        CardFunctions.AD_callback(() => {
            this.看广告解锁格子(0)
        }, Consts.埋点事件.视频广告_开格子);
    }

    async On_解锁格子2() {
        if (GameData.mainState !== MainState.空闲) {
            return;
        }

        CardFunctions.AD_callback(() => {
            this.看广告解锁格子(1);
        }, Consts.埋点事件.视频广告_开格子);
    }

    @Lockable(1000)
    On_Button_replay() {
        director.loadScene("GameScene");
        SS_AudioManager.PlayButtonAudio();
    }

    @Lockable(1000)
    On_Button_setting() {
        SS_UIManager.Instance.CreatePanel(`UI/SettingPanel`, SettingPanel);

        SS_AudioManager.PlayButtonAudio();
    }

    @Lockable(1000)
    async On_Button_随机方块() {
        if (GameData.mainState !== MainState.空闲) {
            return;
        }

        CardFunctions.AD_callback(() => {
            CardFunctions.随机方块();
        }, Consts.埋点事件.视频广告_随机方块);
    }

    @Lockable(1000)
    async On_Button_摧毁障碍() {
        if (GameData.mainState !== MainState.空闲) {
            return;
        }

        await CardFunctions.AD_callback(() => {
            CardFunctions.摧毁方块();
            this.Refresh_Button_摧毁障碍()
        }, Consts.埋点事件.视频广告_摧毁障碍);
    }

    async On_Button_选关() {
        SS_UIManager.Instance.CreatePanel(`UI/MainPanel`, MainPanel);
    }
}

/** 调试 */
window["GamePanel"] = GamePanel;